home *** CD-ROM | disk | FTP | other *** search
- /*
- * the class SAFE_DRAW
- * Copyright (C) 1997 Kazutaka Hirata <khirata@jove.acs.unt.edu>
- */
-
- #include "stdafx.h"
-
- #include "common/base.h"
-
- #include "safedraw.h"
-
- SAFE_DRAW::SAFE_DRAW(CDC* pDC)
- : m_pDC(pDC)
- {
- CRect rect;
- pDC->GetClipBox(&rect);
- m_xmin = rect.left;
- m_xmax = rect.right;
- m_ymin = rect.top;
- m_ymax = rect.bottom;
-
- // Pen
- m_pCurrentPen = new CPen(PS_SOLID, 1, RGB(0, 0, 0));
- m_nPenStyle = PS_SOLID;
- m_nWidth = 1;
- m_crColor = RGB(0, 0, 0);
- m_pOldPen = m_pDC->SelectObject(m_pCurrentPen);
-
- // Brush
- m_pCurrentBrush = new CBrush(RGB(0, 0, 0));
- m_brush_transparent = false;
- m_brush_crColor = RGB(0, 0, 0);
- m_pOldBrush = m_pDC->SelectObject(m_pCurrentBrush);
-
- // XOR
- m_OldXor = m_pDC->SetROP2(R2_XORPEN);
- m_pDC->SetROP2(m_OldXor);
- m_CurrentXor = m_OldXor;
- }
-
- SAFE_DRAW::~SAFE_DRAW(void)
- {
- m_pDC->SetROP2(m_OldXor);
-
- m_pDC->SelectObject(m_pOldBrush);
- delete m_pCurrentBrush;
-
- m_pDC->SelectObject(m_pOldPen);
- delete m_pCurrentPen;
- }
-
- BOOL SAFE_DRAW::is_valid_x(int x) const
- {
- return ((m_xmin <= x) && (x <= m_xmax)) ? TRUE : FALSE;
- }
-
- BOOL SAFE_DRAW::is_valid_y(int y) const
- {
- return ((m_ymin <= y) && (y <= m_ymax)) ? TRUE : FALSE;
- }
-
- BOOL SAFE_DRAW::is_valid(int x, int y) const
- {
- return (is_valid_x(x) && is_valid_y(y)) ? TRUE : FALSE;
- }
-
- void SAFE_DRAW::set_pen(int nPenStyle, int nWidth, COLORREF crColor)
- {
- if((m_nPenStyle != nPenStyle)
- || (m_nWidth != nWidth )
- || (m_crColor != crColor )) {
- CPen* pNewPen = new CPen(nPenStyle, nWidth, crColor);
- CPen* pOldPen = m_pDC->SelectObject(pNewPen);
- delete pOldPen;
- m_pCurrentPen = pNewPen;
- m_nPenStyle = nPenStyle;
- m_nWidth = nWidth;
- m_crColor = crColor;
- }
- }
-
- void SAFE_DRAW::set_brush(int brush_transparent, COLORREF brush_crColor)
- {
- if((m_brush_transparent != brush_transparent)
- || (m_brush_crColor != brush_crColor )) {
- CBrush* pNewBrush;
- if(brush_transparent) {
- pNewBrush = new CBrush;
- LOGBRUSH lb;
- lb.lbStyle = BS_NULL;
- lb.lbColor = 0;
- lb.lbHatch = 0;
- pNewBrush->CreateBrushIndirect(&lb);
- } else {
- pNewBrush = new CBrush(brush_crColor);
- }
- CBrush* pOldBrush = m_pDC->SelectObject(pNewBrush);
- delete pOldBrush;
- m_pCurrentBrush = pNewBrush;
- m_brush_transparent = brush_transparent;
- m_brush_crColor = brush_crColor;
- }
- }
-
- void SAFE_DRAW::set_rop(int Xor)
- {
- if(m_CurrentXor != Xor) {
- m_pDC->SetROP2(Xor);
- m_CurrentXor = Xor;
- }
- }
-
- #define CLIP_X(x) \
- if((x < m_xmin) && (m_xmax < x)) { \
- return; \
- }
-
- #define CLIP_Y(y) \
- if((y < m_ymin) && (m_ymax < y)) { \
- return; \
- }
-
- #define CLIP_XY(x, y) \
- CLIP_X(x); \
- CLIP_X(y);
-
- #define CLIP_XX(x1, x2) \
- if(x2 < m_xmin || m_xmax < x1) { \
- return; \
- }
-
- #define CLIP_YY(y1, y2) \
- if(y2 < m_ymin || m_ymax < y1) { \
- return; \
- }
-
- void SAFE_DRAW::draw_point_core(int x, int y) const
- {
- int xx = x + 1;
- int yy = y + 1;
- m_pDC->MoveTo(x , y );
- m_pDC->LineTo(xx, yy);
- }
-
- void SAFE_DRAW::draw_point(int x, int y, COLORREF color)
- {
- int xx = x + 1;
- int yy = y + 1;
- CLIP_XY(x , y );
- CLIP_XY(xx, yy);
- set_pen(PS_SOLID, 1, color);
- m_pDC->MoveTo(x , y );
- m_pDC->LineTo(xx, yy);
- }
-
- void SAFE_DRAW::draw_plain_circle(int x, int y, int r, COLORREF color, int fill, int xor)
- {
- int x1 = x - r;
- int x2 = x + r;
- CLIP_XX(x1, x2);
- int y1 = y - r;
- int y2 = y + r;
- CLIP_YY(y1, y2);
- set_pen(PS_SOLID, 1, color);
- set_rop(xor ? R2_XORPEN : R2_COPYPEN);
- if(fill) {
- set_brush(false, color);
- } else {
- set_brush(true, RGB(0, 0, 0));
- }
- m_pDC->Ellipse(x1, y1, x2, y2);
- }
-
- void SAFE_DRAW::draw_plain_vertical_line(int x, int y1, int y2, int w, COLORREF color, int xor)
- {
- CLIP_X(x);
- sort2(y1, y2);
- CLIP_YY(y1, y2);
- y1 = max(y1, m_ymin);
- y2 = min(y2, m_ymax);
- set_pen(PS_SOLID, w, color);
- set_rop(xor ? R2_XORPEN : R2_COPYPEN);
- m_pDC->MoveTo(x, y1);
- m_pDC->LineTo(x, y2);
- }
-
- #if 0
- void SAFE_DRAW::draw_direct_vertical_line(int x, int y1, int y2, int w, COLORREF color, int xor)
- {
- set_pen(PS_SOLID, w, color);
- set_rop(xor ? R2_XORPEN : R2_COPYPEN);
- m_pDC->MoveTo(x, y1);
- m_pDC->LineTo(x, y2);
- }
- #endif
-
- void SAFE_DRAW::draw_plain_holizontal_line(int x1, int x2, int y, int w, COLORREF color, int xor)
- {
- CLIP_Y(y);
- sort2(x1, x2);
- CLIP_XX(x1, x2);
- x1 = max(x1, m_xmin);
- x2 = min(x2, m_xmax);
- set_pen(PS_SOLID, w, color);
- set_rop(xor ? R2_XORPEN : R2_COPYPEN);
- m_pDC->MoveTo(x1, y);
- m_pDC->LineTo(x2, y);
- }
-
- void SAFE_DRAW::draw_plain_frame_box(int x1, int y1, int x2, int y2, COLORREF color, int xor)
- {
- draw_plain_holizontal_line(x1, x2, y1, 1, color, xor);
- draw_plain_holizontal_line(x1, x2, y2, 1, color, xor);
- draw_plain_vertical_line (x1, y1, y2, 1, color, xor);
- draw_plain_vertical_line (x2, y1, y2, 1, color, xor);
- }
-
- void SAFE_DRAW::draw_plain_filled_box(int x1, int y1, int x2, int y2, COLORREF color, int xor)
- {
- sort2(x1, x2);
- CLIP_XX(x1, x2);
- sort2(y1, y2);
- CLIP_YY(y1, y2);
-
- x1 = max(x1, m_xmin);
- x2 = min(x2, m_xmax);
- y1 = max(y1, m_ymin);
- y2 = min(y2, m_ymax);
- set_pen(PS_SOLID, 1, color);
- set_brush(false, color);
- set_rop(xor ? R2_XORPEN : R2_COPYPEN);
- m_pDC->Rectangle(x1, y1, x2, y2);
- }
-
- void SAFE_DRAW::draw_plain_box(int x1, int y1, int x2, int y2, COLORREF color, int fill, int xor)
- {
- if(fill) {
- sort2(x1, x2);
- sort2(y1, y2);
- draw_plain_filled_box(x1, y1, x2 + 1, y2 + 1, color, xor);
- } else {
- draw_plain_frame_box(x1, y1, x2, y2, color, xor);
- }
- }
-
- void SAFE_DRAW::draw_plain_line(int x1, int y1, int x2, int y2, int w, COLORREF color, int xor)
- {
- if(x1 == x2) {
- draw_plain_vertical_line(x1, y1, y2, w, color, xor);
- } else if(y1 == y2) {
- draw_plain_holizontal_line(x1, x2, y1, w, color, xor);
- } else {
- #if 0
- if(x2 < x1) {
- swap(x1, x2);
- swap(y1, y2);
- }
- // The following is slightly different from CLIP_XX.
- // Therefore, don't replace it!
- if(x2 <= m_xmin || m_xmax <= x1) {
- return;
- }
- CLIP_YY(min(y1, y2), max(y1, y2));
- if(x1 < m_xmin) {
- int r1 = m_xmin - x1;
- int ra = x2 - x1;
- x1 = m_xmin;
- y1 = y1 + (y2 - y1) * r1 / ra;
- }
- if(m_xmax < x2) {
- int r1 = m_xmax - x1;
- int ra = x2 - x1;
- x2 = m_xmax;
- y2 = y1 + (y2 - y1) * r1 / ra;
- }
- if(y1 != y2) {
- if(y2 < y1) {
- swap(x1, x2);
- swap(y1, y2);
- }
- if(y1 < m_ymin) {
- int r1 = m_ymin - y1;
- int ra = y2 - y1;
- x1 = x1 + (x2 - x1) * r1 / ra;
- y1 = m_ymin;
- }
- if(m_ymax < y2) {
- int r1 = m_ymax - y1;
- int ra = y2 - y1;
- x2 = x1 + (x2 - x1) * r1 / ra;
- y2 = m_ymax;
- }
- }
- #endif
-
- set_pen(PS_SOLID, w, color);
- set_rop(xor ? R2_XORPEN : R2_COPYPEN);
- m_pDC->MoveTo(x1, y1);
- m_pDC->LineTo(x2, y2);
- }
- }
-